<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\upload;

use app\common\services\CacheService;
use app\controller\property\Base;
use api\upload\exception\UploadException;
use app\common\repositories\system\upload\SystemUploadConfigRepository;
use app\validate\property\users\propertyGroupValidate;
use think\App;


class UploadConfig extends Base
{

    protected $repository;

    public function __construct(App $app, SystemUploadConfigRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    /**
     *
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-20 22:40
     */
    public function configUpdate()
    {
        $info = $this->repository->getConfig($this->request->propertyId);
        if (!$info) {
            $param = $this->request->param([
                'type_platform' => 'local',
                'file_type' => 'gif,ico,icon,jpg,jpeg,png',
                'file_size' => '10240000',
            ]);
            $info = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'type_platform' => '',
                'file_type' => '',
                'file_size' => '',
                'bucket_name' => '',
                'regional_node' => '',
                'access_key' => '',
                'key_secret' => '',
                'domain_url' => '',
            ]);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->propertyUId);
                if ($res) {
                    CacheService::create($this->request->propertyId)->delete('upload_config_' . $this->request->propertyId);
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑文件参数失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('upload/update_info', [
                'info' => $info,
            ]);
        }
    }

}